<h2>Why is this an issue?</h2>
<p>Conditional expressions which are always <code>true</code> or <code>false</code> can lead to <a
href="https://en.wikipedia.org/wiki/Unreachable_code">unreachable code</a>.</p>
<p>In the case below, the call of <code>Dispose()</code> never happens.</p>
<pre>
Dim a = False

If a Then
    Dispose() ' Never reached
End If
</pre>
<h3>Exceptions</h3>
<p>This rule will not raise an issue in either of these cases:</p>
<ul>
  <li> When the condition is a single <code>const bool</code> </li>
</ul>
<pre>
Const debug = False
'...
If debug Then
    ' Print something
End If
</pre>
<ul>
  <li> When the condition is the literal <code>true</code> or <code>false</code>. </li>
</ul>
<p>In these cases, it is obvious the code is as intended.</p>
<h2>How to fix it</h2>
<p>The conditions should be reviewed to decide whether:</p>
<ul>
  <li> to update the condition or </li>
  <li> to remove the condition. </li>
</ul>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Public Sub Sample(ByVal b As Boolean)
    Dim a = False
    If a Then                   ' Noncompliant: The true branch is never reached
        DoSomething()           ' Never reached
    End If

    If Not a OrElse b Then      ' Noncompliant: "not a" is always "True" and the false branch is never reached
        DoSomething()
    Else
        DoSomethingElse()       ' Never reached
    End If

    Dim c = "xxx"
    Dim res = If(c, "value")    ' Noncompliant: d is always not Nothing, "value" is never used
End Sub
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
Public Sub Sample(ByVal b As Boolean)
    Dim a = False
    If Foo(a) Then             ' Condition was updated
        DoSomething()
    End If

    If b Then                  ' Parts of the condition were removed.
        DoSomething()
    Else
        DoSomethingElse()
    End If

    Dim c = "xxx"
    Dim res = c                ' "value" was removed
End Sub
</pre>
<h2>Resources</h2>
<ul>
  <li> CWE - <a href="https://cwe.mitre.org/data/definitions/570">CWE-570 - Expression is Always False</a> </li>
  <li> CWE - <a href="https://cwe.mitre.org/data/definitions/571">CWE-571 - Expression is Always True</a> </li>
  <li> Wikipedia - <a href="https://en.wikipedia.org/wiki/Unreachable_code">Unreachable code</a> </li>
</ul>
<h3>Documentation</h3>
<ul>
  <li> Microsoft Learn - <a
  href="https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/logical-and-bitwise-operators">Logical and Bitwise Operators in Visual Basic</a> </li>
  <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators">?.
  and ?() null-conditional operators (Visual Basic)</a> </li>
  <li> Microsoft Learn - <a
  href="https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator#if-operator-called-with-two-arguments">If
  operator called with two arguments</a> </li>
</ul>

